package com.example.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.Quarter;
import cn.hutool.core.date.DateUtil;
import com.example.bean.User;
import com.example.service.UserService;
import com.example.until.Result;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @title: EchartsController
 * @Author wjf
 * @Date: 2022/9/26 10:39
 * @Version 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/echarts")
public class EchartsController {

    @Resource
    private UserService userService;
//    @RequestMapping("/example")
//    public Result getData0(){
//        Map<String,Object> map = new HashMap<>();
//        map.put("x", CollUtil.newArrayList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"));
//        map.put("y",CollUtil.newArrayList(150, 230, 224, 218, 135, 147, 260));
//        return Result.ok(map);
//    }

    @RequestMapping("/data")
    public Result getData() {
        List<User> list = userService.list();
        int q1 = 0;  int q2 = 0;  int q3 = 0; int q4 = 0;
        for (User user : list) {
            Date createTime = user.getCreateTime();
            Quarter quarter = DateUtil.quarterEnum(createTime);
            switch (quarter) {
                case Q1: q1 += 1;
                    break;
                case Q2: q2 += 1;
                    break;
                case Q3: q3 += 1;
                    break;
                case Q4: q4 += 1;
                    break;
            }
        }
        return Result.ok(CollUtil.newArrayList(q1,q2,q3,q4));
    }

    @RequestMapping("/total")
    public Result count(){
        long count = userService.count();
        return Result.ok(count);
    }

}
